/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spärrsonat;

import com.jsyn.JSyn;
import com.jsyn.Synthesizer;
import com.jsyn.data.FloatSample;
import com.jsyn.data.SegmentedEnvelope;
import com.jsyn.unitgen.LineOut;
import com.jsyn.unitgen.LinearRamp;
import com.jsyn.unitgen.UnitOscillator;
import com.jsyn.unitgen.VariableRateMonoReader;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SoundMachine {

    private static final long serialVersionUID = -2704222221111608377L;
    private Synthesizer synt;
    private UnitOscillator osc;
    private LinearRamp lag;
    private LineOut lineOut;
    private final VariableRateMonoReader snuttSpelare;
    private FloatSample snutt;
    private final int FRAMERATE = 44100;
    private final VariableRateMonoReader ampEnvSpelare;
    private final SegmentedEnvelope amplitudEnvelopp;

    /**
     * Klassen som genererar ljud f;r sonaten.
     * 
     */
    public SoundMachine() {

        synt = JSyn.createSynthesizer(); //skapa en jsyn synth.

        //standardljudets l'ngd
        float sampleLength = 1F;
        
        // generera en s[gtand som skall spelas upp.
        int NUM_FRAMES = (int) Math.floor(sampleLength * FRAMERATE); // \r detta en farlig castning?
        float[] sågtandData = new float[NUM_FRAMES];
        float value = 0.0F;
        for (int i = 0; i < sågtandData.length; i++) {
            sågtandData[i] = value;
            value += 0.01;
            if (value >= 2.0) {
                value -= 4.0;
            }
        }
        snutt = new FloatSample(sågtandData);
        snutt.setFrameRate(FRAMERATE);

        // skapa en uppspelare f;r samplen
        snuttSpelare = new VariableRateMonoReader();
        snuttSpelare.rate.set(snutt.getFrameRate());
        snuttSpelare.dataQueue.queueLoop(snutt, 0, snutt.getNumFrames());

        // l'gg till samplen i ut/kanalen
        synt.add(snuttSpelare);
        synt.add(lineOut = new LineOut());
        snuttSpelare.output.connect(0, lineOut.input, 0);
        
        // modulera samplen med en envelope
        // datapar typen l'ngd-ampitud.
        // hela skall vara en sekund.
        double[] ampData = {
                0.05, 0.8, // duration,value pair 0, "attack"
                0.25, 1.0, // pair 1, "sustain"
                0.10, 0.5, // pair 2, "decay"
                0.70, 0.0  // pair 3, "release"
        };
        amplitudEnvelopp = new SegmentedEnvelope( ampData );
        ampEnvSpelare = new VariableRateMonoReader();
        ampEnvSpelare.dataQueue.queue( amplitudEnvelopp );
        synt.add( ampEnvSpelare );
        ampEnvSpelare.output.connect(snuttSpelare.amplitude);
        

        synt.start();
    }

    /**
     * Plays a sound with a corresponding to a given number of passing persons.
     * 
     * @param quantity - the number of arrivals in this 'beat'
     */
    void play(int quantity) {

        //System.out.println("Vill spela"+quantity);
        
        //ladda en envelope s[ att vi f[r fina ;verg[ngar.
        ampEnvSpelare.dataQueue.queue( amplitudEnvelopp );
        
        lineOut.start();
        try {
            Thread.sleep(1000); //sov en sekund
        } catch (InterruptedException ex) {
            Logger.getLogger(SoundMachine.class.getName()).log(Level.SEVERE, null, ex);
        }
        lineOut.stop();
        
    }

    void close() {
        synt.stop();
    }
}